package com.woniu113.junk.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.order.dto.AllAppealInfoDTO;
import com.woniu113.junk.order.dto.AppealInfoDTO;
import com.woniu113.junk.order.dto.ComplaintDTO;
import com.woniu113.junk.order.dto.ComplaintInfoDTO;
import com.woniu113.junk.order.exception.AppealException;
import com.woniu113.junk.order.form.AddAppealInfoForm;
import com.woniu113.junk.order.form.FindAppealInfoForm;
import com.woniu113.junk.order.form.FindComplainInfoForm;
import com.woniu113.junk.order.form.PageForm;
import com.woniu113.junk.order.model.AppealInfo;
import com.woniu113.junk.order.param.AddAppealInfoParam;
import com.woniu113.junk.order.param.FindAppealInfoParam;
import com.woniu113.junk.order.param.FindComplainInfoParam;
import com.woniu113.junk.order.service.AppealInfoService;
import com.woniu113.junk.order.util.Data;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.parser.Token;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/appealInfo")
@Api(tags = "申诉相关接口")
public class AppealInfoController {

    @GetMapping("/test")
    @ApiOperation(value = "测试接口", notes = "用于测试接口的功能")
    public String test(@RequestParam String name) {
        return name;
    }

    @Resource
    private AppealInfoService ais;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增申诉信息
     *
     * @param form 用于新增申诉的表单数据
     * @return 返回操作结果，包含新增的申诉信息
     * @author 小周
     * @date 2024/10/18
     */
    @PostMapping("/appeal")
    @ApiOperation(value = "新增申诉信息", notes = "新增申诉信息并返回结果")
    public Result<AppealInfo> appeal(@RequestBody AddAppealInfoForm form, @RequestHeader String token) throws Exception{
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println("投诉id"+form.getComplaintId());
        System.out.println("投诉内容"+form.getAppealContent());
        System.out.println("当前登录骑手id"+adminId);
        AppealInfo appealInfo =ais.appeal(BeanUtil.toBean(form, AddAppealInfoParam.class),adminId);
        return Result.getResult(AppealInfo.class).setData(appealInfo).setMsg("新增申诉信息成功").setCode(200);
    }

    /**
     * 标记申诉为成功
     *
     * @param appealId 申诉信息的唯一标识
     * @return 返回处理结果，表示申诉成功的状态
     * @throws AppealException 如果申诉信息不存在，则抛出异常
     * @author 小周
     * @date 2024/10/18
     */
    @GetMapping("/over/{appealId}")
    @ApiOperation(value = "标记申诉为成功", notes = "通过申诉ID标记申诉为成功")
    @ApiImplicitParam(name = "appealId", value = "申诉信息的唯一标识", required = true, dataType = "Integer", paramType = "path")
    public Result<AppealInfo> over(@PathVariable Integer appealId) {
        AppealInfo appealInfo = ais.getById(appealId);
        if (appealInfo == null) throw new AppealException("申诉不存在", 5201);
        appealInfo.setResult(Data.APPEAL_INFO_STATE_OVER);
        return Result.getResult(AppealInfo.class).setCode(200).setMsg("申诉结果为成功");
    }

    /**
     * 标记申诉为失败
     *
     * @param appealId 申诉信息的唯一标识
     * @return 返回处理结果，表示申诉失败的状态
     * @throws AppealException 如果申诉信息不存在，则抛出异常
     * @author 小周
     * @date 2024/10/18
     */
    @GetMapping("/fail/{appealId}")
    @ApiOperation(value = "标记申诉为失败", notes = "通过申诉ID标记申诉为失败")
    @ApiImplicitParam(name = "appealId", value = "申诉信息的唯一标识", required = true, dataType = "Integer", paramType = "path")
    public Result<AppealInfo> fail(@PathVariable Integer appealId) {
        AppealInfo appealInfo = ais.getById(appealId);
        if (appealInfo == null) throw new AppealException("申诉不存在", 5201);
        appealInfo.setResult(Data.APPEAL_INFO_STATE_FAIL);
        return Result.getResult(AppealInfo.class).setCode(200).setMsg("申诉结果为失败");
    }

    /**
     * 查询符合条件的申诉内容
     *
     * @param complaintId 投诉信息的唯一标识，以便查找相关的申诉内容
     * @return 返回查询结果，包含符合条件的申诉信息
     * @author 小周
     * @date 2024/10/18
     */
    @GetMapping ("/find")
    @ApiOperation(value = "查询符合条件的申诉内容", notes = "通过投诉ID查询相关的申诉信息")
    @ApiImplicitParam(name = "complaintId", value = "投诉信息的唯一标识", required = true, dataType = "Integer", paramType = "path")
    public Result<AppealInfo> find(@RequestParam("complaintId") Integer complaintId) {
        AppealInfo appealInfo = ais.find(complaintId);
        return Result.getResult(AppealInfo.class).setCode(200).setMsg("查询符合条件的申诉内容成功").setData(appealInfo);
    }

    /**
     * 查看骑手的全部申诉信息
     */
    @PostMapping("/findByRider")
    @ApiOperation(value = "查看骑手的全部申诉信息(骑手查询)", notes = "查看骑手的全部申诉信息(骑手查询)")
    public Result<List<AppealInfoDTO>> findByRider(@RequestBody FindAppealInfoForm form, @RequestHeader String token) throws Exception{
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        System.out.println("当前登录骑手id"+adminId);
        FindAppealInfoParam param = BeanUtil.toBean(form, FindAppealInfoParam.class);
        param.setUserId(adminId);
        AllAppealInfoDTO dto = ais.findByRider(param);
        return Result.getListResult(AppealInfoDTO.class).setMsg("查看骑手的全部申诉信息成功").setData(dto.getList()).setTotal(dto.getTotal());
    }
}

